﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WinnerCMS.Common
{
    /// <summary>
    /// 服务器控件处理类
    /// </summary>
    public class ServerControl
    {
        /// <summary>
        /// 取得服务器控件生成的HTML代码
        /// </summary>
        /// <param name="ctl"></param>
        /// <returns></returns>
        public static string RenderControlAsString(Control ctl)
        {
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                ctl.RenderControl(writer);
             
                return sw.ToString();
            }
        }

        #region 绑定对象数据源

        /// <summary>
        /// 绑定对象数据源
        /// </summary>
        /// <param name="list">DropDownList 、ComboBox 等等继承 ListControl 的数据控件。</param>
        /// <param name="dataSource">数据源。</param>
        /// <param name="textfield">文本。</param>
        /// <param name="valuefield">值。</param>
        /// <param name="firstListItem">第一项是否是空字符串。true是，false否。</param>
        public static void BindDropDownList<T>(ListControl list, T dataSource, string textfield, string valuefield, ListItem firstListItem)
        {
            list.DataSource = dataSource;
            list.DataTextField = textfield;
            list.DataValueField = valuefield;
            list.DataBind();

            if (firstListItem != null)
            {
                list.Items.Insert(0, firstListItem);
            }

        }
        #endregion

        #region 绑定树形控件
        /// <summary>
        /// 绑定树形控件
        /// </summary>
        /// <param name="treeView1"></param>
        /// <param name="dt"></param>
        /// <param name="text"></param>
        /// <param name="value"></param>
        /// <param name="pid"></param>
        /// <param name="rootValue"></param>
        public static void BindTreeView(TreeView treeView1, DataTable dt, string text, string value, string pid, string rootValue)
        {
            BindTreeView(treeView1, dt, text, value, pid, rootValue, string.Empty);
        }
        /// <summary>
        /// 绑定树形控件
        /// </summary>
        /// <param name="treeView1"></param>
        /// <param name="dt"></param>
        /// <param name="text"></param>
        /// <param name="value"></param>
        /// <param name="pid"></param>
        /// <param name="rootValue"></param>
        /// <param name="url"></param>
        public static void BindTreeView(TreeView treeView1, DataTable dt, string text, string value, string pid, string rootValue, string url)
        {
            foreach (DataRow dr in dt.Select(string.Format("{0}='{1}'", pid, rootValue)))
            {
                TreeNode node = new TreeNode(dr[text].ToString(), dr[value].ToString());
                if (url.Length > 0)
                {
                    node.NavigateUrl = dr[url].ToString();
                }
                treeView1.Nodes.Add(node);

                AddNode(treeView1, dt, text, value, pid, dr[value].ToString(), url, node);
            }
        }

        private static void AddNode(TreeView treeView1, DataTable dt, string text, string value, string pid, string pValue, string url, TreeNode tn)
        {
            foreach (DataRow dr in dt.Select(string.Format("{0}='{1}'", pid, pValue)))
            {
                TreeNode node = new TreeNode(dr[text].ToString(), dr[value].ToString());
                if (url.Length > 0)
                {
                    node.NavigateUrl = dr[url].ToString();
                }
                tn.ChildNodes.Add(node);

                AddNode(treeView1, dt, text, value, pid, dr[value].ToString(), url, node);
            }
        }
        #endregion
    }
}